package com.huxun.controller;

import com.huxun.bean.User;
import com.huxun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@Scope("prototype")//默认单例 改成多例
public class UserController {
    @Autowired
    UserService userService;

    /*
    * 跳转登录界面
    * */
    @RequestMapping({"/admin","/login"})
    public String login(){
        return "login";
    }
    //执行登录请求
    @RequestMapping("/user_login")
    public String user_login(String usercode, User user, Model model, HttpSession session) {
        //系统的验证码
        String checkcode = (String) session.getAttribute("checkcode");
        if (checkcode.equalsIgnoreCase(usercode)) {
            System.out.println("表单接收的参数是:" + user);
            User loginUser = userService.login(user);
            if (loginUser != null) {
                session.setAttribute("loginUser", loginUser);
                System.out.println("登录用户是：" + loginUser);
                return "index";
            } else {
                model.addAttribute("message", "账号或密码错误!");
                return "login";
            }
        }else{
            model.addAttribute("message", "用户输入验证码错误!");
            return "login";
        }
    }
    /**
     * 02-用户注销
     * @return
     */
    @RequestMapping("/user_logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }
}
